<?php

declare(strict_types=1);

namespace app\shop\controller\api\commission;

use app\shop\controller\api\ShopBase;
use app\shop\service\commission\Agent as AgentService;
use app\shop\model\commission\Agent as AgentModel;
use sheep\exception\SheepException;

class Commission extends ShopBase
{
    protected AgentService $service;

    public function initialize()
    {
        $on = sheep_config('shop.commission.level');
        if (!$on) {
            throw (new SheepException())->setMessage('分销中心已关闭,该功能暂不可用', 101);
        }
        $user = $this->auth()->user();
        // 检查分销商状态
        $this->service = new AgentService($user);
        if ($this->service->agent && $this->service->agent->status === AgentModel::AGENT_STATUS_FORBIDDEN) {
            throw (new SheepException())->setMessage('账户已被禁用,该功能暂不可用', 102);
        }
    }
}
